package company.permissionsystem.controller;

import company.permissionsystem.model.entity.SysPermission;
import company.permissionsystem.model.entity.SysRole;
import company.permissionsystem.model.entity.SysUser;
import company.permissionsystem.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;

    @GetMapping("/admin")
    @PreAuthorize("hasRole('admin')")
    public String adminList(Model model) {
        List<SysUser> list =  adminService.findAllAdmin();
        model.addAttribute("admins", list);
        return "admin/admin-list";
    }

    @GetMapping("/admin/role")
    @PreAuthorize("hasRole('admin')")
    public String roleList(Model model) {
        List<SysRole> list = adminService.findAllRole();
        model.addAttribute("roles", list);
        return "admin/admin-role";
    }

    @GetMapping("/admin/rule")
    @PreAuthorize("hasRole('admin')")
    public String permissionList(Model model) {
        List<SysPermission> list = adminService.findAllSysPermission();
        model.addAttribute("permissions", list);
        return "admin/admin-rule";
    }


}
